﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HWEngine;
using HWEngine.Generators;

namespace HandWriter.Controls
{
	public static class FontItemDrawHelper
	{
		public static void DrawItem(object sender, DrawItemEventArgs e, IList<string> data, LetterSetRepository fonts)
		{
			if (e.Index < 0)
			{
				return;
			}

			var bc = SystemBrushes.Window;
			var fc = SystemColors.WindowText;
			var fc2 = SystemBrushes.WindowText;
			var lw = 1f;
			if ((e.State | DrawItemState.Selected) == e.State)
			{
				bc = SystemBrushes.Highlight;
				fc = SystemColors.HighlightText;
				fc2 = SystemBrushes.HighlightText;
				lw = 1.4f;
			}

			e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
			e.Graphics.FillRectangle(bc, e.Bounds);

			var systemFontName = e.Font.Name;

			if (fonts != null)
			{
				HWGenerator hwgen = new HWGenerator(
					fonts,
					new RectangleF(e.Bounds.Left + 4, e.Bounds.Top + 3, e.Bounds.Width - 4, 100));

				systemFontName = fonts.Get(data[e.Index]).SystemFontMatchName;

				hwgen.SimpleGenerate(w => w.Draw(e.Graphics),
									data[e.Index],
									systemFontName,
									18,
									fc,
									lw);
			}

			if (e.Bounds.Height > 40)
			{
				using (var f = new Font(systemFontName, 10))
				{
					e.Graphics.DrawString(
						data[e.Index],
						f,
						fc2,
						e.Bounds.Left + 20,
						e.Bounds.Bottom - 20);
				}
			}
			e.DrawFocusRectangle();
		}

		public static void MeasureItem()
		{
		}
	}
}
